//Page Object\
const { request } = require('../../utils/request')
const { formatYMR_cn } = require('../../utils/util')
const { setShareDataStorage } = require('../../utils/storage')
const { getStatusBarAndMenuButtonInfo,splitContentForPage } = require('../../utils/util')
// 获取应用实例
Page({
  data: {
    menuButtonHeight:'',
    statusBarHeight:'',
    menuButtonGutter:'',
    menuBarHeight:'',
    recordData:{
      id:'',
      title:'',
      content:'',
      shareUserNick:'',
      shareImg:'',
      templateCode:'riji'
    },
    contLines:0,
    deleteRecordDialogVisible:false,
  },
  //options(Object)
  onLoad: function(options){
    console.log('options:',options)
    let { menuButtonHeight,statusBarHeight,menuButtonGutter,menuBarHeight} =  getStatusBarAndMenuButtonInfo()
    this.setData({
      menuButtonHeight,
      statusBarHeight,
      menuButtonGutter,
      menuBarHeight,
    })

    setShareDataStorage(options)
    if(options && options.id){
      // 获取分享详情
      this.getShareData(options.id)
      this.setData({
        ['recordData.id']:options.id
      })
    }
  },
  showDeleteDialog(){
    this.setData({
      deleteRecordDialogVisible:true
    })
  },
  getShareData(id:string){
    let that = this
    request({
      url:'/record-share/'+id,
      method:'GET',
      success:(data:any)=>{
        if(data && data.delFlag !==1){
          let pageList = splitContentForPage(data,{
            riji_firstPageRowNum:10,
            riji_otherPageRowNum:11,
            shuxin_firstPageRowNum:9,
            shuxin_otherPageRowNum:10,
            shige_firstPageRowNum:7,
          })
          if(data.templateCode === 'shige'){
            data.contlines = data.content.split('\n').length
          }
          console.log('pageList:',pageList)
          this.setData({
            recordData:{
              ...data,
              pageList
            }
          })
        }else {
          this.showDeleteDialog()
          setTimeout(()=>{
            that.jumpHome()
          },3000)
        }
        
      }
    })
  },
  jumpHome(){
    this.setData({
      deleteRecordDialogVisible:false
    })
    wx.reLaunch({
      url:'/pages/index/index'
    })
    
  },

  getComputedShareData(){
    let userInfo = wx.getStorageSync('userInfo')
    let userId = (userInfo && userInfo.id) || ''
    let currentTemplateCode = this.data.recordData.templateCode
    let title = ''
    if (currentTemplateCode === 'riji') {
      title = '我记了一篇日记，快来看看吧'
    } else if (currentTemplateCode === 'shige') {
      title = '我作了一首诗，快来看看吧'
    } else if (currentTemplateCode === 'shuxin') {
      title = '我写了一封信，快来看看吧'
    }
    if(this.data.recordData.cjFlag == 1){
      title = '我写给你的新年贺词，喜欢吗？'
    }
    return {
      title: title,
      path:'/pages/share/share?inviteUserId='+userId+'&shareChannel=2&id=' + this.data.recordData.id,
      imageUrl:this.data.recordData.shareImg
    }
  
  },
  onShareAppMessage(){
    return this.getComputedShareData()
  },
 
});